package sk.stuba.fiit.clientApp.gui.panel;

import javax.swing.JPanel;
import javax.swing.JComboBox;


import javax.swing.JTextArea;
import javax.swing.JButton;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JTabbedPane;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JCheckBox;
import javax.swing.JTable;

import sk.stuba.fiit.clientApp.controller.ClientUtil;
import sk.stuba.fiit.clientApp.gui.handler.AddButtonFileActionListener;
import sk.stuba.fiit.clientApp.gui.handler.AddSignatureToVerifyActionListener;
import sk.stuba.fiit.clientApp.gui.handler.ArchiveExecuteActionListener;
import sk.stuba.fiit.clientApp.gui.handler.OpenInputFileListener;
import sk.stuba.fiit.clientApp.gui.handler.PreferenceExecuteActionListener;
import sk.stuba.fiit.clientApp.gui.handler.RemoveButtonFileActionListener;
import sk.stuba.fiit.clientApp.gui.handler.SignActionListener;
import sk.stuba.fiit.clientApp.gui.handler.VerifyActionListener;
import sk.stuba.fiit.clientApp.gui.model.FileTableModel;
import sk.stuba.fiit.clientApp.util.InitializationUtil;


import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import javax.swing.ScrollPaneConstants;
import javax.swing.JList;

/**
 * GUI rozhranie klienta
 * @author Administrator
 *
 */
public class BasicPanel extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPasswordField passwordField;
	private JTextField textFieldCert;
	private JTable table;
	private final ButtonGroup buttonGroup = new ButtonGroup();
	private JComboBox jcomboSignMethod;
	private JComboBox jcomboHashMethod;
	private JComboBox jcomboSignPolicy;
	private JTextArea console;
	private JCheckBox chboxAllData;
	private JTextField textFieldPrivateKey;
	private JRadioButton rdbtnPkcs8;
	private JLabel lblCertificate;
	private JButton btnCert;
	private JRadioButton rdbtnPkcs12;
	private JRadioButton rdbtnJks;
	private JTextField textFieldOutput;
	private JButton btnPrivateKey;
	private JTextField tfApplication;
	private JTextField tfArchivalPackage;
	private JTable tableVerify;
	private JTextField tfUserID;
	private JTextField tfUserName;
	private JPasswordField tfUserPassword;
	private JTextField tfToken;
	private JTextField tfEmail;
	private JTextField tfPackage;
	private JTextField tfPreferencies;
	private JRadioButton rdbtnFromApplication;
	private JRadioButton rdbtnFromAs;
	private JButton btnBrowseAP;
	private JButton btnSignature;
	private JButton btnAddRefVerify;
	private JButton btnRemoveReference;
	private JComboBox comboBoxArchive;
	private JComboBox comboBoxPreference;
	private JCheckBox chckbxSend;
	private JCheckBox chckbxSendRenew;
	private JButton btnBrowseArch;
	private JLabel lblPreferencies;
	private JButton btnPreferencies;
	private JLabel symbol;
	private JLabel status;
	private JTextArea consolePref;

	public JTextArea getConsole() {
		return console;
	}

	public void setConsole(JTextArea console) {
		this.console = console;
	}

	/**
	 * Create the panel.
	 */
	public BasicPanel() {
		setLayout(new BorderLayout(0, 0));
		
		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		add(tabbedPane, BorderLayout.CENTER);
		
		JPanel panel = new JPanel();
		tabbedPane.addTab("Sign", null, panel, null);
		panel.setLayout(null);
		
		JLabel lblSignatureAlgorithm = new JLabel("Signature Algorithm:");
		lblSignatureAlgorithm.setBounds(10, 161, 150, 14);
		panel.add(lblSignatureAlgorithm);
		
		JLabel lblHashAlgorithm = new JLabel("Hash Algorithm:");
		lblHashAlgorithm.setBounds(10, 186, 150, 14);
		panel.add(lblHashAlgorithm);
		
		//TODO implementacia bezpecnostnej politiky
//		JLabel lblSignaturePolicy = new JLabel("Signature Policy:");
//		lblSignaturePolicy.setBounds(10, 246, 150, 14);
//		panel.add(lblSignaturePolicy);
		
		lblCertificate = new JLabel("All data TS:");
		lblCertificate.setBounds(363, 161, 75, 14);
		panel.add(lblCertificate);
		
		JLabel lblIncludeCreationTime = new JLabel("Include creation time:");
		lblIncludeCreationTime.setBounds(10, 218, 150, 14);
		panel.add(lblIncludeCreationTime);
		
		JLabel lblPassword = new JLabel("Password:");
		lblPassword.setBounds(363, 218, 75, 14);
		panel.add(lblPassword);
		
		passwordField = new JPasswordField();
		passwordField.setEchoChar('*');
		passwordField.setBounds(493, 215, 150, 20);
		panel.add(passwordField);
		
		jcomboSignMethod = new JComboBox();
		jcomboSignMethod.setBounds(170, 158, 183, 20);
		jcomboSignMethod.setModel(InitializationUtil.getSignatureMethod());
		panel.add(jcomboSignMethod);
		
		jcomboHashMethod = new JComboBox();
		jcomboHashMethod.setBounds(170, 183, 183, 20);
		jcomboHashMethod.setModel(InitializationUtil.getHashMethod());
		panel.add(jcomboHashMethod);
		
		//TODO pridanie bezpecnostnej politiky
//		jcomboSignPolicy = new JComboBox();
//		jcomboSignPolicy.setBounds(170, 243, 183, 20);
//		jcomboSignPolicy.setModel(InitializationUtil.getSignaturePolicy());
//		panel.add(jcomboSignPolicy);
		
		textFieldCert = new JTextField();
		textFieldCert.setEditable(false);
		textFieldCert.setBounds(493, 158, 300, 20);
		panel.add(textFieldCert);
		textFieldCert.setColumns(10);
		
		btnCert = new JButton("Attached TS");
		btnCert.setBounds(803, 157, 105, 23);
		btnCert.addActionListener(new OpenInputFileListener(textFieldCert,null,true,ClientUtil.TS_FILE_VALU));
		panel.add(btnCert);
		
		chboxAllData = new JCheckBox("Yes");
		chboxAllData.setBounds(171, 214, 97, 23);
		panel.add(chboxAllData);
		
		table = new JTable(new FileTableModel());
		JScrollPane jsp =  new JScrollPane(table);
		jsp.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		jsp.setBounds(159, 11, 749, 127);
		panel.add(jsp);
		
		JButton btnaddfile = new JButton("Add File");
		btnaddfile.addActionListener(new AddButtonFileActionListener(table));
		btnaddfile.setBounds(29, 35, 106, 23);
		panel.add(btnaddfile);
		
		JButton btnRemoveFile = new JButton("Remove File");
		btnRemoveFile.addActionListener(new RemoveButtonFileActionListener(table));
		btnRemoveFile.setBounds(29, 69, 106, 23);
		panel.add(btnRemoveFile);
		
		rdbtnPkcs12 = new JRadioButton("PKCS#12");
		FileFormatActionListener fileFormatActionListener = new FileFormatActionListener();
		
		rdbtnJks = new JRadioButton("JKS");
		rdbtnJks.setSelected(true);
		rdbtnJks.addActionListener(fileFormatActionListener);
		buttonGroup.add(rdbtnJks);
		rdbtnJks.setBounds(493, 238, 75, 23);
		panel.add(rdbtnJks);
		
		rdbtnPkcs12.addActionListener(fileFormatActionListener);
		buttonGroup.add(rdbtnPkcs12);
		rdbtnPkcs12.setBounds(570, 238, 89, 23);
		panel.add(rdbtnPkcs12);
		
		//TODO neimplementovana funkcnost
		rdbtnPkcs12.setVisible(false);
		rdbtnPkcs12.setEnabled(false);
		
		JLabel lblCertificateFormat = new JLabel("Private Key Format:");
		lblCertificateFormat.setBounds(363, 242, 106, 14);
		panel.add(lblCertificateFormat);
		
		rdbtnPkcs8 = new JRadioButton("PKCS#8");
		rdbtnPkcs8.addActionListener(fileFormatActionListener);
		buttonGroup.add(rdbtnPkcs8);
		rdbtnPkcs8.setBounds(661, 237, 71, 23);
		panel.add(rdbtnPkcs8);
		
		//TODO neimplementovana funkcnost
		rdbtnPkcs8.setVisible(false);
		rdbtnPkcs8.setEnabled(false);
		
		JButton btnSign = new JButton("Sign");
		btnSign.addActionListener(new SignActionListener(this));
		btnSign.setBounds(803, 240, 105, 23);
		panel.add(btnSign);
		
		JPanel verifyPanel = new JPanel();
		tabbedPane.addTab("Verify", null, verifyPanel, null);
		verifyPanel.setLayout(null);
		
		
		VerifyInitActionListener verifyButonlistener = new VerifyInitActionListener();
		rdbtnFromAs = new JRadioButton("Verify signature from archival system");
		rdbtnFromAs.setBounds(27, 7, 275, 23);
		rdbtnFromAs.addActionListener(verifyButonlistener);
		verifyPanel.add(rdbtnFromAs);
		
		tfApplication = new JTextField();
		tfApplication.setBounds(204, 97, 559, 20);
		verifyPanel.add(tfApplication);
		tfApplication.setColumns(10);
		
		tableVerify = new JTable(new FileTableModel());
		JScrollPane jspVerify =  new JScrollPane(tableVerify);
		jspVerify.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		jspVerify.setBounds(204, 128, 559, 118);
		verifyPanel.add(jspVerify);
		
		btnAddRefVerify = new JButton("Add reference");
		btnAddRefVerify.setBounds(47, 128, 147, 23);
		btnAddRefVerify.addActionListener(new AddButtonFileActionListener(tableVerify));
		verifyPanel.add(btnAddRefVerify);
		
		rdbtnFromApplication = new JRadioButton("Verify signature from application");
		rdbtnFromApplication.setBounds(27, 67, 275, 23);
		rdbtnFromApplication.addActionListener(verifyButonlistener);
		rdbtnFromApplication.setSelected(true);
		verifyPanel.add(rdbtnFromApplication);
		
		ButtonGroup bgVerify = new ButtonGroup();
		bgVerify.add(rdbtnFromAs);
		bgVerify.add(rdbtnFromApplication);
		
		tfArchivalPackage = new JTextField();
		tfArchivalPackage.setBounds(200, 37, 563, 20);
		verifyPanel.add(tfArchivalPackage);
		tfArchivalPackage.setColumns(10);
		
		btnBrowseAP = new JButton("Browse");
		btnBrowseAP.setBounds(780, 36, 128, 23);
		btnBrowseAP.addActionListener(new OpenInputFileListener(tfArchivalPackage,null,true,ClientUtil.ARCHIVE_PACKAGE_VALUE));
		verifyPanel.add(btnBrowseAP);
		
		JLabel lblSignature = new JLabel("Signature location:");
		lblSignature.setBounds(76, 100, 118, 14);
		verifyPanel.add(lblSignature);
		
		JLabel lblArchivkoPackage = new JLabel("Archival package");
		lblArchivkoPackage.setBounds(76, 37, 114, 14);
		verifyPanel.add(lblArchivkoPackage);
		
		JButton btnVerify = new JButton("Verify signature");
		btnVerify.setBounds(779, 264, 128, 23);
		verifyPanel.add(btnVerify);
		btnVerify.addActionListener(new VerifyActionListener(this));
		
		btnSignature = new JButton("Browse");
		btnSignature.setBounds(780, 96, 128, 23);
		verifyPanel.add(btnSignature);
		btnSignature.addActionListener(new AddSignatureToVerifyActionListener(tfApplication,this,null));
		
		btnRemoveReference = new JButton("Remove reference");
		btnRemoveReference.setBounds(47, 162, 147, 23);
		btnRemoveReference.addActionListener(new RemoveButtonFileActionListener(tableVerify));
		verifyPanel.add(btnRemoveReference);
		
		symbol = new JLabel(".");
		symbol.setBounds(555, 257, 46, 41);
		verifyPanel.add(symbol);
		
		status = new JLabel(" ");
		status.setBounds(204, 273, 317, 14);
		verifyPanel.add(status);
		
		JPanel panel_2 = new JPanel();
		tabbedPane.addTab("Archive", null, panel_2, null);
		panel_2.setLayout(null);
		
		comboBoxArchive = new JComboBox();
		comboBoxArchive.setModel(InitializationUtil.getArchiveOperation());
		comboBoxArchive.setBounds(119, 11, 341, 20);
		comboBoxArchive.addActionListener(new ArchiveActionListener());
		panel_2.add(comboBoxArchive);
		
		JLabel lblOperation = new JLabel("Operation: ");
		lblOperation.setBounds(26, 14, 83, 14);
		panel_2.add(lblOperation);
		
		JLabel lblUserName = new JLabel("User name:");
		lblUserName.setBounds(119, 82, 80, 14);
		panel_2.add(lblUserName);
		
		JLabel lblUserPassword = new JLabel("User password:");
		lblUserPassword.setBounds(119, 107, 131, 14);
		panel_2.add(lblUserPassword);
		
		JLabel lblUserId = new JLabel("User ID:");
		lblUserId.setBounds(119, 56, 80, 14);
		panel_2.add(lblUserId);
		
		tfUserID = new JTextField(InitializationUtil.getUserId());
		tfUserID.setBounds(260, 53, 200, 20);
		panel_2.add(tfUserID);
		tfUserID.setColumns(10);
		
		tfUserName = new JTextField(InitializationUtil.getUserName());
		tfUserName.setBounds(260, 79, 200, 20);
		panel_2.add(tfUserName);
		tfUserName.setColumns(10);
		
		tfUserPassword = new JPasswordField(InitializationUtil.getUserPassword());
		tfUserPassword.setBounds(260, 104, 200, 20);
		panel_2.add(tfUserPassword);
		tfUserPassword.setColumns(10);
		
		JLabel lblToken = new JLabel("Token:");
		lblToken.setBounds(119, 163, 46, 14);
		panel_2.add(lblToken);
		
		tfToken = new JTextField();
		tfToken.setBounds(260, 160, 200, 20);
		panel_2.add(tfToken);
		tfToken.setColumns(10);
		
		JButton btnExecuteArch = new JButton("Execute");
		btnExecuteArch.setBounds(701, 261, 89, 23);
		btnExecuteArch.addActionListener(new ArchiveExecuteActionListener(this));
		panel_2.add(btnExecuteArch);
		
		JLabel lblPackage = new JLabel("Package:");
		lblPackage.setBounds(119, 228, 116, 14);
		panel_2.add(lblPackage);
		
		tfEmail = new JTextField();
		tfEmail.setBounds(260, 188, 200, 20);
		panel_2.add(tfEmail);
		tfEmail.setColumns(10);
		
		JLabel lblEMail = new JLabel("E - mail");
		lblEMail.setBounds(119, 191, 46, 14);
		panel_2.add(lblEMail);
		
		chckbxSend = new JCheckBox("Send archive package");
		chckbxSend.setBounds(405, 261, 209, 23);
		panel_2.add(chckbxSend);
		
		 chckbxSendRenew = new JCheckBox("Send renew package");
		chckbxSendRenew.setBounds(119, 261, 181, 23);
		panel_2.add(chckbxSendRenew);
		
		tfPackage = new JTextField();
		tfPackage.setBounds(260, 225, 400, 20);
		panel_2.add(tfPackage);
		tfPackage.setColumns(10);
		
		btnBrowseArch = new JButton("Browse");
		btnBrowseArch.setBounds(701, 224, 89, 23);
		btnBrowseArch.addActionListener(new OpenInputFileListener(tfPackage, null));
		panel_2.add(btnBrowseArch);
		
		
		
		JPanel panel_3 = new JPanel();
		tabbedPane.addTab("Preferences", null, panel_3, null);
		panel_3.setLayout(null);
		
		comboBoxPreference = new JComboBox();
		comboBoxPreference.setBounds(213, 5, 275, 20);
		panel_3.add(comboBoxPreference);
		comboBoxPreference.setModel(InitializationUtil.getPrefOperation());
		comboBoxPreference.addActionListener(new PreferenceInitialActionListener());
		
		JLabel lblOperation_1 = new JLabel("Operation:");
		lblOperation_1.setBounds(80, 8, 123, 14);
		panel_3.add(lblOperation_1);
		
		lblPreferencies = new JLabel("Action: ");
		lblPreferencies.setBounds(80, 40, 134, 14);
		panel_3.add(lblPreferencies);
		
		tfPreferencies = new JTextField();
		tfPreferencies.setBounds(213, 37, 467, 20);
		panel_3.add(tfPreferencies);
		tfPreferencies.setColumns(10);
		
		JButton btnExecute_1 = new JButton("Execute");
		btnExecute_1.setBounds(79, 65, 89, 23);
		btnExecute_1.addActionListener(new PreferenceExecuteActionListener(this));
		panel_3.add(btnExecute_1);
		
		btnPreferencies = new JButton("Browse");
		btnPreferencies.setBounds(706, 36, 89, 23);
		btnPreferencies.addActionListener(new OpenInputFileListener(tfPreferencies, null));
		panel_3.add(btnPreferencies);
		
		consolePref = new JTextArea();
		consolePref.setRows(15);
		JScrollPane jspConsolePref =  new JScrollPane(consolePref);
		jspConsolePref.setSize(715, 186);
		jspConsolePref.setLocation(80, 100);
		jspConsolePref.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		jspConsolePref.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
		panel_3.add(jspConsolePref);
	
		console = new JTextArea();
		console.setRows(8);
		JScrollPane jspConsole =  new JScrollPane(console);
		jsp.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		jsp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
		
		textFieldPrivateKey = new JTextField();
		textFieldPrivateKey.setEditable(false);
		textFieldPrivateKey.setBounds(493, 183, 300, 20);
		panel.add(textFieldPrivateKey);
		textFieldPrivateKey.setColumns(10);
		
		JLabel lblPrivateKey = new JLabel("Private Key:");
		lblPrivateKey.setBounds(363, 186, 78, 14);
		panel.add(lblPrivateKey);
		
		btnPrivateKey = new JButton("Private Key");
		btnPrivateKey.addActionListener(new OpenInputFileListener(textFieldPrivateKey,null));
		btnPrivateKey.setBounds(803, 182, 105, 23);
		panel.add(btnPrivateKey);
		
		JLabel lblOutputPackage = new JLabel("Output package:");
		lblOutputPackage.setBounds(170, 274, 105, 14);
		panel.add(lblOutputPackage);
		
		textFieldOutput = new JTextField();
		textFieldOutput.setBounds(266, 271, 459, 20);
		panel.add(textFieldOutput);
		textFieldOutput.setColumns(10);
		
		JButton btnBrowse = new JButton("Browse");
		btnBrowse.addActionListener(new OpenInputFileListener(textFieldOutput, null,true,ClientUtil.OUTPUT_PACKAGE_VALUE));
		btnBrowse.setBounds(803, 270, 105, 23);
		panel.add(btnBrowse);
		add(jspConsole, BorderLayout.SOUTH);
		
		initbtnGroup();
		initVerifySection();
		initArchiveActionListener();
		initPreferenciesListener();
	}

	public JCheckBox getChboxAllData() {
		return chboxAllData;
	}

	public void setChboxAllData(JCheckBox chboxAllData) {
		this.chboxAllData = chboxAllData;
	}

	public JPasswordField getPasswordField() {
		return passwordField;
	}

	public JTextField getTextFieldPrivateKey() {
		return textFieldPrivateKey;
	}

	public void setTextFieldPrivateKey(JTextField textFieldPrivateKey) {
		this.textFieldPrivateKey = textFieldPrivateKey;
	}

	public void setPasswordField(JPasswordField passwordField) {
		this.passwordField = passwordField;
	}

	public JTextField getTextFieldCert() {
		return textFieldCert;
	}

	public void setTextFieldCert(JTextField textField) {
		this.textFieldCert = textField;
	}

	public JTable getTable() {
		return table;
	}

	public void setTable(JTable table) {
		this.table = table;
	}

	public JComboBox getJcomboSignMethod() {
		return jcomboSignMethod;
	}

	public void setJcomboSignMethod(JComboBox jcomboSignMethod) {
		this.jcomboSignMethod = jcomboSignMethod;
	}

	public JComboBox getJcomboHashMethod() {
		return jcomboHashMethod;
	}

	public void setJcomboHashMethod(JComboBox jcomboHashMethod) {
		this.jcomboHashMethod = jcomboHashMethod;
	}

	public JComboBox getJcomboSignPolicy() {
		return jcomboSignPolicy;
	}

	public void setJcomboSignPolicy(JComboBox jcomboSignPolicy) {
		this.jcomboSignPolicy = jcomboSignPolicy;
	}
	
	private void initbtnGroup(){
//		if(rdbtnPkcs8.isSelected()){
//			btnCert.setVisible(true);
//		}else{
//			btnCert.setVisible(false);
//		}		
		textFieldCert.setText("");
		textFieldPrivateKey.setText("");
		passwordField.setText("");
	}
	
	private void initVerifySection (){
		((FileTableModel) getTableVerify().getModel()).makeEmptyModel();
		if(rdbtnFromAs.isSelected()){
			tfArchivalPackage.setEnabled(true);
			btnBrowseAP.setEnabled(true);
			
			tfApplication.setEnabled(false);
			tfApplication.setText("");
			btnSignature.setEnabled(false);
			btnAddRefVerify.setEnabled(false);
			btnRemoveReference.setEnabled(false);
		}else{
			tfArchivalPackage.setEnabled(false);
			tfArchivalPackage.setText("");
			btnBrowseAP.setEnabled(false);
			
			tfApplication.setEnabled(true);
			btnSignature.setEnabled(true);
			btnAddRefVerify.setEnabled(true);
			btnRemoveReference.setEnabled(true);
		}
	}
	
	private void initPreferenciesListener(){
		String value = (String) comboBoxPreference.getModel().getSelectedItem();
		tfPreferencies.setText("");
		switch(value.charAt(0)){
		case '1':
			lblPreferencies.setText("Data to timestamp:");
			btnPreferencies.setText("Browse");
			btnPreferencies.setEnabled(true);
			lblPreferencies.setEnabled(true);
			break;
		case '2':
			lblPreferencies.setText("Certificate SN:");
			btnPreferencies.setEnabled(false);
			lblPreferencies.setEnabled(true);
			break;
		case '3':
			lblPreferencies.setText("");
			lblPreferencies.setEnabled(false);
			btnPreferencies.setText("");
			btnPreferencies.setEnabled(false);
			break;
		case '4':
			btnPreferencies.setEnabled(true);
			lblPreferencies.setEnabled(true);
			lblPreferencies.setText("TimeStamp location");
			btnPreferencies.setText("Browse");
			break;
		case '5':
			btnPreferencies.setEnabled(true);
			lblPreferencies.setEnabled(true);
			lblPreferencies.setText("Certificate location");
			btnPreferencies.setText("Browse");
			break;
		case '6':
			btnPreferencies.setEnabled(true);
			lblPreferencies.setEnabled(true);
			lblPreferencies.setText("CRL location");
			btnPreferencies.setText("Browse");
			break;
		default:
			JOptionPane.showMessageDialog(null, "Unsupported operation!","Waring",JOptionPane.WARNING_MESSAGE);
		}
		
	}
	
	private void initArchiveActionListener(){
		String value = (String) comboBoxArchive.getModel().getSelectedItem();
		setClearArchiveField();
		setVisibleArchiveField(false);
		switch(value.charAt(0)){
		case '1':
			tfEmail.setEnabled(true);
			tfPackage.setEnabled(true);
			chckbxSend.setEnabled(true);
			chckbxSendRenew.setEnabled(true);
			btnBrowseArch.setEnabled(true);
			break;
		case '2':
		case '3':tfToken.setEnabled(true);
			break;
		case 'X':
			tfUserID.setEnabled(true);
			tfUserName.setEnabled(true);
			tfUserPassword.setEnabled(true);
			break;
		default:
			JOptionPane.showMessageDialog(null, "Unsupported operation!","Waring",JOptionPane.WARNING_MESSAGE);
		}
	}
	
	private void setVisibleArchiveField(boolean enabled){
		tfUserID.setEnabled(enabled);
		tfUserName.setEnabled(enabled);
		tfUserPassword.setEnabled(enabled);
		tfToken.setEnabled(enabled);
		tfEmail.setEnabled(enabled);
		tfPackage.setEnabled(enabled);
		chckbxSend.setEnabled(enabled);
		btnBrowseArch.setEnabled(enabled);
		chckbxSendRenew.setEnabled(enabled);
	}
	
	private void setClearArchiveField(){
		tfToken.setText("");
		tfEmail.setText("");
		tfPackage.setText("");
		chckbxSend.setSelected(false);
		chckbxSendRenew.setSelected(false);		
	}
	
	
	class FileFormatActionListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			initbtnGroup();
		}
	}
	
	class VerifyInitActionListener implements ActionListener{
		
		@Override
		public void actionPerformed(ActionEvent e) {
			initVerifySection();	
		}
	}
	
	class ArchiveActionListener implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
			initArchiveActionListener();
		}
	}
	
	class PreferenceInitialActionListener implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
			initPreferenciesListener();
		}
	}

	public String getSelectedFormat() {
		if(rdbtnPkcs8.isSelected()){
			return "pkcs8";
		}else if(rdbtnPkcs12.isSelected()){
			return "pkcs12";
		}else{
			return "jks";
		}
	}

	public boolean validateRequiredFields() {
		if( this.getTable().getModel().getRowCount() <= 0  ){
			JOptionPane.showMessageDialog(null,"Data for signature are not selected! ","Error",JOptionPane.ERROR_MESSAGE);
			return false;
		}
		if(textFieldPrivateKey.getText() == null || textFieldPrivateKey.getText().equals("")){
			JOptionPane.showMessageDialog(null,"When using a private key to sign data it must be select a private key to create signature","Error",JOptionPane.ERROR_MESSAGE);
			return false;
		}
		
		if(rdbtnPkcs8.isSelected() && (textFieldCert.getText() == null || textFieldCert.getText().equals(""))){
			JOptionPane.showMessageDialog(null,"When using a private key in PKCS # 8 it must be select a certificate corresponding public key","Error",JOptionPane.ERROR_MESSAGE);
			return false;
		}
		if((rdbtnPkcs12.isSelected() || rdbtnJks.isSelected()) && 
				(passwordField.getPassword()== null || passwordField.getText().equals(""))){
			JOptionPane.showMessageDialog(null,"Password for private key is required!","Error",JOptionPane.ERROR_MESSAGE);
		return false;
		}
		
//		TODO iny format
//		if(!rdbtnPkcs8.isSelected()){
//			textFieldCert.setText(textFieldPrivateKey.getText());
//		}
	
		if(textFieldOutput.getText().equals("")){
			File file = new File(ClientUtil.OUTPUT_PACKAGE);
			textFieldOutput.setText(file.getAbsolutePath());
			JOptionPane.showMessageDialog(null,"You don't have selected the output file, it will be used the current directory","Error",JOptionPane.INFORMATION_MESSAGE);
		}
	return true;
	}

	public JTextField getTextFieldOutput() {
		return textFieldOutput;
	}

	public void setTextFieldOutput(JTextField textFieldOutput) {
		this.textFieldOutput = textFieldOutput;
	}

	public JTable getTableVerify() {
		return tableVerify;
	}

	public void setTableVerify(JTable tableVerify) {
		this.tableVerify = tableVerify;
	}

	public JRadioButton getRdbtnFromAs() {
		return rdbtnFromAs;
	}

	public void setRdbtnFromAs(JRadioButton rdbtnFromAs) {
		this.rdbtnFromAs = rdbtnFromAs;
	}

	public JTextField getTfApplication() {
		return tfApplication;
	}

	public void setTfApplication(JTextField tfApplication) {
		this.tfApplication = tfApplication;
	}

	public JTextField getTfArchivalPackage() {
		return tfArchivalPackage;
	}

	public void setTfArchivalPackage(JTextField tfArchivalPackage) {
		this.tfArchivalPackage = tfArchivalPackage;
	}

	public JLabel getSymbol() {
		return symbol;
	}

	public void setSymbol(JLabel symbol) {
		this.symbol = symbol;
	}

	public JLabel getStatus() {
		return status;
	}

	public void setStatus(JLabel status) {
		this.status = status;
	}

	public JComboBox getComboBoxArchive() {
		return comboBoxArchive;
	}

	public void setComboBoxArchive(JComboBox comboBoxArchive) {
		this.comboBoxArchive = comboBoxArchive;
	}

	public JTextField getTfUserID() {
		return tfUserID;
	}

	public void setTfUserID(JTextField tfUserID) {
		this.tfUserID = tfUserID;
	}

	public JTextField getTfUserName() {
		return tfUserName;
	}

	public void setTfUserName(JTextField tfUserName) {
		this.tfUserName = tfUserName;
	}

	public JTextField getTfUserPassword() {
		return tfUserPassword;
	}

	public void setTfUserPassword(JPasswordField tfUserPassword) {
		this.tfUserPassword = tfUserPassword;
	}

	public JComboBox getComboBoxPreference() {
		return comboBoxPreference;
	}

	public void setComboBoxPreference(JComboBox comboBoxPreference) {
		this.comboBoxPreference = comboBoxPreference;
	}

	public JTextField getTfEmail() {
		return tfEmail;
	}

	public void setTfEmail(JTextField tfEmail) {
		this.tfEmail = tfEmail;
	}

	public JTextField getTfPackage() {
		return tfPackage;
	}

	public void setTfPackage(JTextField tfPackage) {
		this.tfPackage = tfPackage;
	}

	public JCheckBox getChckbxSend() {
		return chckbxSend;
	}

	public void setChckbxSend(JCheckBox chckbxSend) {
		this.chckbxSend = chckbxSend;
	}

	public JCheckBox getChckbxSendRenew() {
		return chckbxSendRenew;
	}

	public void setChckbxSendRenew(JCheckBox chckbxSendRenew) {
		this.chckbxSendRenew = chckbxSendRenew;
	}

	public JTextField getTfToken() {
		return tfToken;
	}

	public void setTfToken(JTextField tfToken) {
		this.tfToken = tfToken;
	}

	public JTextField getTfPreferencies() {
		return tfPreferencies;
	}

	public void setTfPreferencies(JTextField tfPreferencies) {
		this.tfPreferencies = tfPreferencies;
	}

	public JTextArea getConsolePref() {
		return consolePref;
	}

	public void setConsolePref(JTextArea consolePref) {
		this.consolePref = consolePref;
	}
}
